<?php
class Application_Model_ResultroundMapper
{
    protected $_dbTable;
    public static $_hashmap = array('Misaka Mikoto' => '御坂美琴', 
    'Tachibana Kanade' => '立华奏', 'Nakano Azusa' => '中野梓', 
    'Nagato Yuki' => '长门有希', 'Shana' => '夏娜', 'Gokō Ruri (Kuroneko)' => '黑猫', 
    'Eucliwood Hellscythe' => '优库里伍德', 'Akiyama Mio' => '秋山澪', 
    'Aisaka Taiga' => '逢坂大河', 'Katsura Hinagiku' => '桂雏菊', 'Yui' => '由依', 
    'Charlotte Dunois' => '夏洛特', 'Nakamura Yuri' => '仲村由理', 
    'Suzumiya Haruhi' => '凉宫春日', 'Saber' => 'Saber', 'Sengoku Nadeko' => '千石抚子', 
    'Senjōgahara Hitagi' => '战场原黑仪', 'Victorique de Blois' => '维多利加', 
    'Sakagami Tomoyo' => '坂上智代', 'Kōsaka Kirino' => '高坂桐乃', 
    'Hirasawa Yui' => '平泽唯', 'Sanzenin Nagi' => '三千院凪', 
    'Fujibayashi Kyō' => '藤林杏', 'Hiiragi Kagami' => '柊镜', 
    'Konjiki no Yami' => '金色之暗', 'Izumi Konata' => '泉此方', 
    'Furukawa Nagisa' => '古河渚', 'Aragaki Ayase' => '新垣绫濑', 
    'Asahina Mikuru' => '朝比奈实玖瑠', 'Kuroi Mato' => '黑岩射手', 'Holo' => '赫萝', 
    'C.C.' => 'C.C.', 'Index L. Prohibitorum' => '茵蒂克丝', 
    'Louise Vallière' => '露易丝', 'Kotobuki Tsumugi' => '琴吹紬', 
    'Fate Testarossa' => '菲特', 'Shiina Mafuyu' => '椎名真冬', 'Hirasawa Ui' => '平泽忧', 
    'Last Order' => '最后之作', 'Shirai Kuroko' => '白井黑子', 'Haruna' => '春奈', 
    'Tōsaka Rin' => '远坂凛', 'Iwasawa Asami' => '岩泽麻美', 'Nymph' => '尼姆芙', 
    'Shinonono Hōki' => '筱之之箒', 'Seraphim' => '瑟拉芬', 'Huang Lingyin' => '黄玲音', 
    'Kasugano Sora' => '春日野穹', 'Shiina' => '椎名', 'Laura Bodewig' => '萝拉');
    public static $_colormap = array('Misaka Mikoto' => '0000FF', 
    'Tachibana Kanade' => '0000F0', 'Nakano Azusa' => '00000F', 
    'Nagato Yuki' => 'F00000', 'Shana' => 'FF0000', 
    'Gokō Ruri (Kuroneko)' => '0F0000', 'Eucliwood Hellscythe' => '000F00', 
    'Akiyama Mio' => '00FF00', 'Aisaka Taiga' => '00F000', 
    'Katsura Hinagiku' => '00FFF0', 'Yui' => '00FF0F', 
    'Charlotte Dunois' => '00F0FF', 'Nakamura Yuri' => '000FFF', 
    'Suzumiya Haruhi' => '00FFFF', 'Saber' => '0FFFF0', 
    'Sengoku Nadeko' => 'FFFF00', 'Senjōgahara Hitagi' => '0FFFFF', 
    'Victorique de Blois' => 'FFFFF0', 'Sakagami Tomoyo' => '0F0F0F', 
    'Kōsaka Kirino' => 'F0F0F0', 'Hirasawa Yui' => 'FF0F0F', 
    'Sanzenin Nagi' => 'FFF0F0', 'Fujibayashi Kyō' => 'FFF00F', 
    'Hiiragi Kagami' => '0F0FFF');
    public function setDbTable ($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (! $dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }
    public function getDbTable ()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Resultround');
        }
        return $this->_dbTable;
    }
    public function getResult ($startRound, $endRound, $names)
    {
        $imageSrc = "http://chart.apis.google.com/chart?" . "chs=640x300" .
         "&cht=lxy";
        $chdl = "";
        $chco = "";
        $chd = "";
        $chds = "&chds=1,10,";
        $chxr = "&chxr=0," . $startRound . "," . $endRound . "|1,0,";
        $chxt = "&chxt=x,y";
        $max = 0;
        $min = 999999999999;
        foreach ($names as $name) {
            if (strlen($chdl) != 0) {
                $chdl = $chdl . "|" . self::$_hashmap[$name];
            } else {
                $chdl = $chdl . self::$_hashmap[$name];
            }
            $color = self::$_colormap[$name];
            if ($color == null) {
                $color = "000000";
            }
            if (strlen($chco) != 0) {
                $chco = $chco . "," . $color;
            } else {
                $chco = $chco . $color;
            }
            $resultSet = $this->getDbTable()->fetchAll(
            $this->getDbTable()
                ->select()
                ->where("round>=?", $startRound)
                ->where("round<=?", $endRound)
                ->where("name=?", $name));
            if (strlen($chd) != 0) {
                $chd = $chd . "|" . "-1|";
            } else {
                $chd = $chd . "-1|";
            }
            $data = "";
            foreach ($resultSet as $row) {
                $vf = $row->vf;
                if (strlen($data) != 0) {
                    $data = $data . "," . $vf;
                } else {
                    $data = $data . $vf;
                }
                if ($vf > $max) {
                    $max = $vf;
                }
                if ($vf < $min) {
                    $min = $vf;
                }
                 //                $entry = new Application_Model_Resultround();
            //                $entry->setName(self::$_hashmap[$row->name])
            //                    ->setRound($row->round)
            //                    ->setName($row->name)
            //                    ->setOpponent($row->opponent)
            //                    ->setVf($row->vf)
            //                    ->setVa($row->va);
            }
            $chd = $chd . $data;
        }
        $chdl = "&chdl=" . $chdl;
        $chco = "&chco=" . $chco;
        $chd = "&chd=t:" . $chd;
        $chxr = $chxr . $max;
        $chds = $chds . $min . "," . $max;
        $imageSrc = $imageSrc . $chdl . $chd . $chds . $chxr . $chxt . $chco;
        return $imageSrc;
    }
}

